package com.sugar.queryinfo.module.business.controller.admin.tffinance;

import com.sugar.queryinfo.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.controller.admin.tffinance.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.tffinance.TfFinanceDO;
import com.sugar.queryinfo.module.business.service.tffinance.TfFinanceService;

@Tag(name = "管理后台 - 真假财务")
@RestController
@RequestMapping("/business/tf-finance")
@Validated
public class TfFinanceController {

    @Resource
    private TfFinanceService tfFinanceService;

    @PostMapping("/create")
    @Operation(summary = "创建真假财务")
    @PreAuthorize("@ss.hasPermission('business:tf-finance:create')")
    public CommonResult<Long> createTfFinance(@Valid @RequestBody TfFinanceSaveReqVO createReqVO) {
        return success(tfFinanceService.createTfFinance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新真假财务")
    @PreAuthorize("@ss.hasPermission('business:tf-finance:update')")
    public CommonResult<Boolean> updateTfFinance(@Valid @RequestBody TfFinanceSaveReqVO updateReqVO) {
        tfFinanceService.updateTfFinance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除真假财务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:tf-finance:delete')")
    public CommonResult<Boolean> deleteTfFinance(@RequestParam("id") Long id) {
        tfFinanceService.deleteTfFinance(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除真假财务")
                @PreAuthorize("@ss.hasPermission('business:tf-finance:delete')")
    public CommonResult<Boolean> deleteTfFinanceList(@RequestParam("ids") List<Long> ids) {
        tfFinanceService.deleteTfFinanceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得真假财务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:tf-finance:query')")
    public CommonResult<TfFinanceRespVO> getTfFinance(@RequestParam("id") Long id) {
        TfFinanceDO tfFinance = tfFinanceService.getTfFinance(id);
        return success(BeanUtils.toBean(tfFinance, TfFinanceRespVO.class));
    }

    @PostMapping("/get/ids")
    @Operation(summary = "获取真假财务根据id列表")
    @PreAuthorize("@ss.hasPermission('business:tf-finance:query')")
    public CommonResult<List<TfFinanceRespVO>> getTfFinancesByIds(@Valid @RequestBody TfFinanceIdsReqVO idsReqVO) {
        if (idsReqVO.getIds() == null || idsReqVO.getIds().isEmpty()) {
            return success(Collections.emptyList());
        }
        return success(tfFinanceService.getTfFinancesByIds(idsReqVO.getIds()));
    }

    @GetMapping("/page")
    @Operation(summary = "获得真假财务分页")
    @PreAuthorize("@ss.hasPermission('business:tf-finance:query')")
    public CommonResult<PageResult<TfFinanceRespVO>> getTfFinancePage(@Valid TfFinancePageReqVO pageReqVO) {
        PageResult<TfFinanceDO> pageResult = tfFinanceService.getTfFinancePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TfFinanceRespVO.class));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入")
    @PreAuthorize("@ss.hasPermission('business:tf-finance:export')")
    public CommonResult<Boolean> importExcel(FileUploadReqVO uploadReqVO) throws IOException {
        tfFinanceService.importExcel(uploadReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出真假财务 Excel")
    @PreAuthorize("@ss.hasPermission('business:tf-finance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTfFinanceExcel(@Valid TfFinancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TfFinanceDO> list = tfFinanceService.getTfFinancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "真假财务.xls", "数据", TfFinanceRespVO.class,
                        BeanUtils.toBean(list, TfFinanceRespVO.class));
    }

}